****************************************************************************
*Delivering remote learning using a low-tech solution: Evidence from an RCT during school closures, Liang Choon Wang, Michael Vlassopoulos, Asad Islam, and Hashibul Hassan, Journal of Political Economy: Micro, 2.3 (2024): 562–601.
****************************************************************************

*Preparing merged dataset for data analysis
pwd

*Bring Random Sample
use "Sample", clear

*Merge data received from GDRI's completed project
cd "1. External Data"
merge 1:1 CHILD_ID using "ECD-Data"
cd ..
drop if _merge != 3
drop _merge

*Merge Rapid Baseline Survey
cd "2. Rapid Baseline Survey"
merge 1:1 CHILD_ID using "Rapid_Baseline_Survey"
cd ..
drop _merge

**************************************
*regenerate variables - Baseline
**************************************
*Child Grade Recode
gen child_grade_std = child_grade
replace child_grade_std = 1 if child_grade == 0
label variable child_grade_std "RECODE of child_grade (In which class this child is studying in 2021)"

*Endline literacy and numeracy are the baseline for this project
rename end_literacy_score baseline_literacy_score
sum baseline_literacy_score
replace baseline_literacy_score = r(mean) if baseline_literacy_score == . 
label var baseline_literacy_score "Baseline literacy score"

rename end_numeracy_score baseline_numeracy_score
sum baseline_numeracy_score
replace baseline_numeracy_score = r(mean) if baseline_numeracy_score == . 
label var baseline_numeracy_score "Baseline numeracy score"

*Father's age 
rename bs21F3 father_age 
sum father_age
replace father_age = r(mean) if father_age == .

***Replace Father's Education into Year of Education
gen F5n=bs21F5
replace F5n=0 if bs21F5==1 | bs21F5==10
replace F5n=2 if bs21F5==2 
replace F5n=5 if bs21F5==3 
replace F5n=8 if bs21F5==4
replace F5n=10 if bs21F5==5 
replace F5n=11 if bs21F5==6
replace F5n=12 if bs21F5==7 
replace F5n=13 if bs21F5==8 
replace F5n=15 if bs21F5==9
label variable F5n "Father's education in years" 

sum F5n 
replace F5n = r(mean) if F5n == .
rename F5n father_edu_years

*Mother's age 
rename bs21M3 mother_age 
sum mother_age
replace mother_age = r(mean) if mother_age == .

*** Replace Father's Education into Year of Education
gen M5n=bs21M5
replace M5n=0 if bs21M5==1 | bs21M5==10
replace M5n=2 if bs21M5==2 
replace M5n=5 if bs21M5==3 
replace M5n=8 if bs21M5==4
replace M5n=10 if bs21M5==5 
replace M5n=11 if bs21M5==6
replace M5n=12 if bs21M5==7 
replace M5n=13 if bs21M5==8 
replace M5n=15 if bs21M5==9
label variable M5n "Mother's education in years" 

sum M5n 
replace M5n = r(mean) if M5n == .
rename M5n mother_edu_years

*Father's profession
rename bs21F6 father_profession 

*Mobile access
rename hh_asset1 mobile_access
rename hh_asset2 smartphone
label var smartphone "Access to smartphone"
rename hh_asset3 television 
label var television "Access to TV"

*Homestead land
rename sc2_17_Q3_15_1 hs_land
sum hs_land
replace hs_land = r(mean) if hs_land == .

*Family income 
egen family_income = rowtotal(bs21F7 bs21M7)
label var family_income "Total income of the HH"

keep CHILD_ID RECORD_ID VILLAGE_ID FAMILY_ID treatment_arms standard_treat extended_treat control_group any_treat child_gen child_age child_school child_grade_std baseline_literacy_score baseline_numeracy_score child_pvt_tuition father_age father_edu_years mother_age mother_edu_years father_profession hh_mem mobile_access smartphone television hs_land family_income

**************************************
*Merge - Endline
**************************************
*merge enline children assessment 
cd "3. Endline Survey"
merge 1:1 CHILD_ID using "Anushilon-Endline-Assessment.dta"
rename _merge merge_var_assessment

*merge endline 1 parental survey
merge 1:1 CHILD_ID using "Anushilon-Endline-Parent-Survey.dta"
cd .. 
rename _merge merge_var_parentsurvey
drop if merge_var_parentsurvey==2

*survey completion indicator
gen endline1_complete=1 if merge_var_assessment==3 & merge_var_parentsurvey==3
replace endline1_complete=0 if merge_var_assessment==1 | merge_var_parentsurvey==1

**************************************
*regenerate variables - Endline 
**************************************
*regligion dummy 
gen reli_dummy= anu_hh_reli==1

*attrition variable 
gen attrit = endline1_complete == 0

rename endline1_complete endline_completed

*Variable Modification Item Response theory 
gen e1_item1 	= 1 if anu_e1_cog_1 > 0 & anu_e1_cog_1 != .
gen e1_item2 	= 1 if anu_e1_cog_2 > 0 & anu_e1_cog_2 != . 
gen e1_item3 	= 1 if anu_e1_cog_3 > 0 & anu_e1_cog_3 != . 
gen e1_item4 	= 1 if anu_e1_cog_4 > 0 & anu_e1_cog_4 != .
gen e1_item5 	= 1 if anu_e1_cog_9 > 0 & anu_e1_cog_9 != .
gen e1_item6 	= 1 if anu_e1_cog_10 > 0 & anu_e1_cog_10 != . 
gen e1_item7 	= 1 if anu_e1_cog_11 > 0 & anu_e1_cog_11 != . 
gen e1_item8 	= 1 if anu_e1_cog_12 > 0 & anu_e1_cog_12 != .
gen e1_item9 	= 1 if anu_e1_cog_13 > 0 & anu_e1_cog_13 != .
gen e1_item10 	= 1 if anu_e1_cog_14 > 0 & anu_e1_cog_14 != .
gen e1_item11 	= 1 if anu_e1_cog_15 > 0 & anu_e1_cog_15 != .
gen e1_item12 	= 1 if anu_e1_cog_16 > 0 & anu_e1_cog_16 != . 
gen e1_item13 	= 1 if anu_e1_cog_17 > 0 & anu_e1_cog_17 != . 
gen e1_item14 	= 1 if anu_e1_cog_18 > 0 & anu_e1_cog_18 != .  
gen e1_item15 	= 1 if anu_e1_cog_19 > 0 & anu_e1_cog_19 != . 

replace e1_item1 	= 0 if anu_e1_cog_1 == 0 & anu_e1_cog_1 != .
replace e1_item2 	= 0 if anu_e1_cog_2 == 0 & anu_e1_cog_2 != . 
replace e1_item3 	= 0 if anu_e1_cog_3 == 0 & anu_e1_cog_3 != . 
replace e1_item4 	= 0 if anu_e1_cog_4 == 0 & anu_e1_cog_4 != .
replace e1_item5 	= 0 if anu_e1_cog_9 == 0 & anu_e1_cog_9 != .
replace e1_item6 	=  0 if anu_e1_cog_10 == 0 & anu_e1_cog_10 != . 
replace e1_item7 	= 0 if anu_e1_cog_11 == 0 & anu_e1_cog_11 != . 
replace e1_item8 	= 0 if anu_e1_cog_12 == 0 & anu_e1_cog_12 != .
replace e1_item9 	= 0 if anu_e1_cog_13 == 0 & anu_e1_cog_13 != .
replace e1_item10 	= 0 if anu_e1_cog_14 == 0 & anu_e1_cog_14 != .
replace e1_item11 	= 0 if anu_e1_cog_15 == 0 & anu_e1_cog_15 != .
replace e1_item12 	= 0 if anu_e1_cog_16 == 0 & anu_e1_cog_16 != . 
replace e1_item13 	= 0 if anu_e1_cog_17 == 0 & anu_e1_cog_17 != . 
replace e1_item14 	= 0 if anu_e1_cog_18 == 0 & anu_e1_cog_18 != .  
replace e1_item15 	= 0 if anu_e1_cog_19 == 0 & anu_e1_cog_19 != . 

gen endline_total_correct = e1_item1 + e1_item2 + e1_item3 + e1_item4 + e1_item5 + e1_item6 + e1_item7 + e1_item8 + e1_item9 + e1_item10 + e1_item11 + e1_item12 + e1_item13 + e1_item14 + e1_item15

label variable endline_total_correct "Total no of correct answer"
forvalues i=1/15{
	label variable e1_item`i' "Endline assessment question `i'"
}

*Renaming variables for cognitive outcome 
rename anu_e1_cog_1			e1_cog_1
rename anu_e1_cog_2			e1_cog_2 
rename anu_e1_cog_3			e1_cog_3 
rename anu_e1_cog_4			e1_cog_4 
rename anu_e1_cog_9			e1_cog_9 
rename anu_e1_cog_10		e1_cog_10 
rename anu_e1_cog_11		e1_cog_11 
rename anu_e1_cog_12		e1_cog_12 
rename anu_e1_cog_13		e1_cog_13 
rename anu_e1_cog_14		e1_cog_14 
rename anu_e1_cog_15		e1_cog_15 
rename anu_e1_cog_16		e1_cog_16 
rename anu_e1_cog_17		e1_cog_17 
rename anu_e1_cog_18		e1_cog_18 
rename anu_e1_cog_19		e1_cog_19

rename anu_e1_cog_tot		endline_total_score 
gen anu_e1_cog_lit = anu_e1_cog_lit_bng + anu_e1_cog_lit_eng 

rename anu_e1_cog_lit 		endline_literacy_score
rename anu_e1_cog_num		endline_numeracy_score

*Standardization of variables
foreach v in endline_total_score endline_literacy_score endline_numeracy_score  anu_edu_22 anu_pi_9 anu_edu_15 anu_pi_4 grit gms affective_empathy cognitive_empathy prosocial_empathy impulsivity_overall impulsivity_schoolwork impulsivity_interpersonal sdq_emotion sdq_conduct sdq_hyper sdq_peer sdq_prosoc leadership communication planning {
sum `v' if treatment_arms == 3
gen `v'_cmean = r(mean)
gen `v'_csd= r(sd)
gen `v'_std= (`v' - `v'_cmean)/`v'_csd
drop `v'_cmean `v'_csd 
}

rename endline_total_score_std endline_total_score_std

rename endline_literacy_score_std endline_literacy_score_std

rename endline_numeracy_score_std endline_numeracy_score_std

*rename variable
rename sdq_prosoc			sdq_prosocial 
rename sdq_prosoc_std		sdq_prosocial_std
rename anu_edu_22			study_time_child
rename anu_edu_22_std		study_time_child_std
rename anu_pi_9				homeschooling_time
rename anu_pi_9_std			homeschooling_time_std
rename anu_edu_15			extent_of_study
rename anu_edu_15_std		extent_of_study_std
rename anu_pi_4				extent_of_homeschooling
rename anu_pi_4_std			extent_of_homeschooling_std
rename anu_edu_6			child_catch_up
rename anu_edu_7			teacher_effort 

*labelling 
label var leadership_std "Parent reported - Leadership skill - standardized"
label var communication_std "Parent reported - Communication skill - standardized"
label var planning_std "Parent reported - planning skill - standardized"

label var sdq_emotion_std "SDQ - Emotional Symptoms - standardized"
label var sdq_conduct_std "SDQ - conduct problems - standardized"
label var sdq_hyper_std "SDQ - hyperactivity/inattention - standardized"
label var sdq_peer_std 	"SDQ - peer problems - standardized"
label var sdq_prosocial_std "SDQ - prosocial - standardized"

label var impulsivity_overall_std "Overall impulsivity - standardized"
label var impulsivity_schoolwork_std "Schoolwork impulsivity - standardized"
label var impulsivity_interpersonal_std "Interpersonal impulsivity - standardized"

label var affective_empathy_std "Affective Empathy (Contagion) - standardized"
label var cognitive_empathy_std "Cognitive Empathy (Understanding) - standardized"
label var prosocial_empathy_std "Prosocial Motivation (Support) - standardized"

label var gms_std "Grwoth Mindset Scale - standardized"
label var grit_std "Grit Scale - standardize"
label var extent_of_study_std "In last few weeks, How much time your child spend for studying? - standardized"
label var study_time_child_std  "Study time, total - standardized"
label var extent_of_homeschooling_std "In the past weeks, how much time you gave in child's education? - standardized"
label var homeschooling_time_std "Daily parental time in education – total - standardized"

label var endline_literacy_score "Children's literacy score of endline assessment"
label var endline_total_score_std "Children's standardized literacy score of endline assessment"
label var endline_literacy_score_std "Children's standardized numeracy score of endline assessment"
label var endline_numeracy_score_std "Children's standardized total score of endline assessment"

label var endline_completed "Dummy of HHs who completed endline survey & assessment"
label var attrit "Dummy of HHs who attrited at the endline"
label var reli_dummy "Household's religion"

**************************************
*Merge - Lesson data
**************************************
*merge enline children assessment 
cd "3. Lesson Data"
merge 1:1 CHILD_ID using "IVR_Lessons_Data.dta"
cd .. 
drop _merge 

**************************************
*New dummy variables
**************************************
todummy family_income if endline_completed == 1, median
label var d_family_income "Median dummy of family income"

gen parent_edu = father_edu_years + mother_edu_years
label var parent_edu "Parent education combined"

todummy parent_edu if endline_completed == 1, median
label var d_parent_edu "Median dummy of parent education"

xtile parent_edu_qrt = parent_edu, nquantiles(4)
label var parent_edu_qrt "Quartile of parent education"

todummy baseline_literacy_score if endline_completed == 1, median
gen low_base_lit = 1 if d_baseline_literacy_score == 0 
replace low_base_lit = 0 if d_baseline_literacy_score == 1 
drop d_baseline_literacy_score
label var low_base_lit "Median dummy of baseline literacy score"

todummy baseline_numeracy_score if endline_completed == 1, median
gen low_base_num = 1 if d_baseline_numeracy_score == 0 
replace low_base_num = 0 if d_baseline_numeracy_score == 1 
drop d_baseline_numeracy_score
label var low_base_num "Median dummy of baseline numeracy score"

gen low_parent_edu = 1 if d_parent_edu == 0 
replace low_parent_edu = 0 if d_parent_edu == 1 
label var low_parent_edu "Reverse median dummy of baseline parent education"

gen low_family_inc = 1 if d_family_income == 0 
replace low_family_inc = 0 if d_family_income == 1 
label var low_family_inc "Reverse Median dummy of baseline family income"

**************************************
*Ordering all variables
**************************************

order CHILD_ID RECORD_ID VILLAGE_ID FAMILY_ID treatment_arms standard_treat extended_treat control_group any_treat endline_completed attrit child_gen child_age child_school child_grade_std baseline_literacy_score baseline_numeracy_score child_pvt_tuition father_age father_edu_years mother_age mother_edu_years father_profession hh_mem mobile_access smartphone television hs_land family_income reli_dummy e1_cog_1 e1_cog_2 e1_cog_3 e1_cog_4 e1_cog_9 e1_cog_10 e1_cog_11 e1_cog_12 e1_cog_13 e1_cog_14 e1_cog_15 e1_cog_16 e1_cog_17 e1_cog_18 e1_cog_19 endline_literacy_score endline_numeracy_score endline_total_score endline_total_score_std endline_literacy_score_std endline_numeracy_score_std e1_item1 e1_item2 e1_item3 e1_item4 e1_item5 e1_item6 e1_item7 e1_item8 e1_item9 e1_item10 e1_item11 e1_item12 e1_item13 e1_item14 e1_item15 endline_total_correct extent_of_study study_time_child extent_of_homeschooling homeschooling_time extent_of_study_std study_time_child_std extent_of_homeschooling_std homeschooling_time_std social_desire_child social_desire_mother grit grit_std gms gms_std affective_empathy cognitive_empathy prosocial_empathy affective_empathy_std cognitive_empathy_std prosocial_empathy_std impulsivity_overall impulsivity_schoolwork impulsivity_interpersonal impulsivity_overall_std impulsivity_schoolwork_std impulsivity_interpersonal_std sdq_emotion sdq_conduct sdq_hyper sdq_peer sdq_prosocial sdq_emotion_std sdq_conduct_std sdq_hyper_std sdq_peer_std sdq_prosocial_std leadership communication planning leadership_std communication_std planning_std child_catch_up teacher_effort svy_missing svyIVR_type evercomp_lit evercomp_num evercomp_lead complete_lit_1 complete_lit_2 complete_lit_3 complete_lit_4 complete_lit_5 complete_lit_6 complete_lit_7 complete_lit_8 complete_lit_9 complete_lit_10 complete_lit_11 complete_lit_12 complete_lit_13 complete_lit_14 complete_lit_15 complete_lit_16 complete_lit_17 complete_lit_18 complete_lit_19 complete_lit_20 complete_lit_21 complete_lit_22 complete_lit_23 complete_lit_24 complete_lit_25 complete_lit_26 complete_lit_27 complete_lit_28 complete_lit_29 complete_lit_30 complete_num_1 complete_num_2 complete_num_3 complete_num_4 complete_num_5 complete_num_6 complete_num_7 complete_num_8 complete_num_9 complete_num_10 complete_num_11 complete_num_12 complete_num_13 complete_num_14 complete_num_15 complete_num_16 complete_num_17 complete_num_18 complete_num_19 complete_num_20 complete_num_21 complete_num_22 complete_num_23 complete_num_24 complete_num_25 complete_num_26 complete_num_27 complete_num_28 complete_num_29 complete_num_30 complete_lead_1 complete_lead_2 complete_lead_3 complete_lead_4 complete_lead_5 complete_lead_6 complete_lead_7 complete_lead_8 complete_lead_9 complete_lead_10 complete_lead_11 complete_lead_12 complete_lead_13 complete_lead_14 complete_lead_15 repeat_lit_1 repeat_lit_2 repeat_lit_3 repeat_lit_4 repeat_lit_5 repeat_lit_6 repeat_lit_7 repeat_lit_8 repeat_lit_9 repeat_lit_10 repeat_lit_11 repeat_lit_12 repeat_lit_13 repeat_lit_14 repeat_lit_15 repeat_lit_16 repeat_lit_17 repeat_lit_18 repeat_lit_19 repeat_lit_20 repeat_lit_21 repeat_lit_22 repeat_lit_23 repeat_lit_24 repeat_lit_25 repeat_lit_26 repeat_lit_27 repeat_lit_28 repeat_lit_29 repeat_lit_30 repeat_num_1 repeat_num_2 repeat_num_3 repeat_num_4 repeat_num_5 repeat_num_6 repeat_num_7 repeat_num_8 repeat_num_9 repeat_num_10 repeat_num_11 repeat_num_12 repeat_num_13 repeat_num_14 repeat_num_15 repeat_num_16 repeat_num_17 repeat_num_18 repeat_num_19 repeat_num_20 repeat_num_21 repeat_num_22 repeat_num_23 repeat_num_24 repeat_num_25 repeat_num_26 repeat_num_27 repeat_num_28 repeat_num_29 repeat_num_30 matchphone totlessons_lit totlessons_num totlessons_tot totlessons_lead rep_totlessons_lit rep_totlessons_num rep_totlessons_tot reppct_lit reppct_num reppct_tot sequence_lit sequence_num cbin_lit cbin_num cbin_tot d_family_income parent_edu d_parent_edu parent_edu_qrt low_base_lit low_base_num low_parent_edu low_family_inc

******************************************************
*Keep only those variables available in IVR_Data file
******************************************************

keep CHILD_ID RECORD_ID VILLAGE_ID FAMILY_ID treatment_arms standard_treat extended_treat control_group any_treat endline_completed attrit child_gen child_age child_school child_grade_std baseline_literacy_score baseline_numeracy_score child_pvt_tuition father_age father_edu_years mother_age mother_edu_years father_profession hh_mem mobile_access smartphone television hs_land family_income reli_dummy e1_cog_1 e1_cog_2 e1_cog_3 e1_cog_4 e1_cog_9 e1_cog_10 e1_cog_11 e1_cog_12 e1_cog_13 e1_cog_14 e1_cog_15 e1_cog_16 e1_cog_17 e1_cog_18 e1_cog_19 endline_literacy_score endline_numeracy_score endline_total_score endline_total_score_std endline_literacy_score_std endline_numeracy_score_std e1_item1 e1_item2 e1_item3 e1_item4 e1_item5 e1_item6 e1_item7 e1_item8 e1_item9 e1_item10 e1_item11 e1_item12 e1_item13 e1_item14 e1_item15 endline_total_correct extent_of_study study_time_child extent_of_homeschooling homeschooling_time extent_of_study_std study_time_child_std extent_of_homeschooling_std homeschooling_time_std social_desire_child social_desire_mother grit grit_std gms gms_std affective_empathy cognitive_empathy prosocial_empathy affective_empathy_std cognitive_empathy_std prosocial_empathy_std impulsivity_overall impulsivity_schoolwork impulsivity_interpersonal impulsivity_overall_std impulsivity_schoolwork_std impulsivity_interpersonal_std sdq_emotion sdq_conduct sdq_hyper sdq_peer sdq_prosocial sdq_emotion_std sdq_conduct_std sdq_hyper_std sdq_peer_std sdq_prosocial_std leadership communication planning leadership_std communication_std planning_std child_catch_up teacher_effort svy_missing svyIVR_type evercomp_lit evercomp_num evercomp_lead complete_lit_1 complete_lit_2 complete_lit_3 complete_lit_4 complete_lit_5 complete_lit_6 complete_lit_7 complete_lit_8 complete_lit_9 complete_lit_10 complete_lit_11 complete_lit_12 complete_lit_13 complete_lit_14 complete_lit_15 complete_lit_16 complete_lit_17 complete_lit_18 complete_lit_19 complete_lit_20 complete_lit_21 complete_lit_22 complete_lit_23 complete_lit_24 complete_lit_25 complete_lit_26 complete_lit_27 complete_lit_28 complete_lit_29 complete_lit_30 complete_num_1 complete_num_2 complete_num_3 complete_num_4 complete_num_5 complete_num_6 complete_num_7 complete_num_8 complete_num_9 complete_num_10 complete_num_11 complete_num_12 complete_num_13 complete_num_14 complete_num_15 complete_num_16 complete_num_17 complete_num_18 complete_num_19 complete_num_20 complete_num_21 complete_num_22 complete_num_23 complete_num_24 complete_num_25 complete_num_26 complete_num_27 complete_num_28 complete_num_29 complete_num_30 complete_lead_1 complete_lead_2 complete_lead_3 complete_lead_4 complete_lead_5 complete_lead_6 complete_lead_7 complete_lead_8 complete_lead_9 complete_lead_10 complete_lead_11 complete_lead_12 complete_lead_13 complete_lead_14 complete_lead_15 repeat_lit_1 repeat_lit_2 repeat_lit_3 repeat_lit_4 repeat_lit_5 repeat_lit_6 repeat_lit_7 repeat_lit_8 repeat_lit_9 repeat_lit_10 repeat_lit_11 repeat_lit_12 repeat_lit_13 repeat_lit_14 repeat_lit_15 repeat_lit_16 repeat_lit_17 repeat_lit_18 repeat_lit_19 repeat_lit_20 repeat_lit_21 repeat_lit_22 repeat_lit_23 repeat_lit_24 repeat_lit_25 repeat_lit_26 repeat_lit_27 repeat_lit_28 repeat_lit_29 repeat_lit_30 repeat_num_1 repeat_num_2 repeat_num_3 repeat_num_4 repeat_num_5 repeat_num_6 repeat_num_7 repeat_num_8 repeat_num_9 repeat_num_10 repeat_num_11 repeat_num_12 repeat_num_13 repeat_num_14 repeat_num_15 repeat_num_16 repeat_num_17 repeat_num_18 repeat_num_19 repeat_num_20 repeat_num_21 repeat_num_22 repeat_num_23 repeat_num_24 repeat_num_25 repeat_num_26 repeat_num_27 repeat_num_28 repeat_num_29 repeat_num_30 matchphone totlessons_lit totlessons_num totlessons_tot totlessons_lead rep_totlessons_lit rep_totlessons_num rep_totlessons_tot reppct_lit reppct_num reppct_tot sequence_lit sequence_num cbin_lit cbin_num cbin_tot d_family_income parent_edu d_parent_edu parent_edu_qrt low_base_lit low_base_num low_parent_edu low_family_inc

*save IVR_Data.dta, replace 

cd ..
cd "2. Data Analysis"
cd "Data"
save IVR_Data.dta, replace 
cd .. 
cd ..
cd "1. Data Preparation"